package com.jt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import com.jt.service.UserService;
import com.jt.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 说明：根据用户名密码判断数据是否有效期
     * 1. 明文加密  md5/md5Hash/sha1/sha16
     * 2. 根据加密后的数据查询数据库
     * 3. 判断结果是否有效
     * 有效：返回秘钥 token = 123
     * null： 返回null
     * UUID 3.4*10^38种可能  算法 ： hash(时间戳 + 随机数)
     */
    @Override
    @Transactional
    public String login(User user) {
        String password = user.getPassword();//1. 获取原始密码
        String md5Str = DigestUtils.md5DigestAsHex(password.getBytes());//2. 密码加密
        user.setPassword(md5Str);//3. 加密文件传给对象
        User user1 = userMapper.selectOne(new QueryWrapper<>(user));//4. 查询数据库
        String uuid = UUID.randomUUID().toString().replace("-", "");//5. 生产随机秘钥UUID
        return user1 == null ? null : uuid;
    }

    /**
     * MySQL的分页语法：limit 起始位置,每页条数   (n-1)*m , m
     */
//    @Override
//    public PageResult getUserList(PageResult pageResult) {
//        //获取总记录数total
//        long total = userMapper.selectCount(null);
//        //获取分页后的结果
////        int startIndex = (pageResult.getPageNum()-1)* pageResult.getPageSize();
////        List<User> rows = userMapper.getUserList(startIndex,pageResult.getPageSize());
//        List<User> rows = userMapper.getUserList02(pageResult);
//        pageResult.setRows(rows).setTotal(total);
//        return pageResult;
//    }

    @Override
    public PageResult getUserList(PageResult pageResult) {
        //1. 定义MP分页对象
        IPage iPage = new Page<>(pageResult.getPageNum(), pageResult.getPageSize());
        //2. 条件构造器
        QueryWrapper queryWrapper = new QueryWrapper();
        //设置flag动态拼接
        boolean flag = StringUtils.hasLength(pageResult.getQuery());
        queryWrapper.like(flag, "username", pageResult.getQuery());
        //经过MP查询将分页数据（total/结果/页面/条数/xxx）封装到iPage对象
        iPage = userMapper.selectPage(iPage, queryWrapper);
        List rows = iPage.getRecords();
        long total = iPage.getTotal();
        return pageResult.setRows(rows).setTotal(total);
    }

    @Override
    @Transactional
    public void updateStatus(User user) {
        //int i = 1/0;
        userMapper.updateById(user);
    }

    @Override
    @Transactional
    public void addUser(User user) {
        String password = user.getPassword();//1. 获取原始密码
        String md5Str = DigestUtils.md5DigestAsHex(password.getBytes());//2. 密码加密
        user.setPassword(md5Str)//3. 加密文件传给对象
            .setStatus(true);
        userMapper.insert(user);
    }

    @Override
    public Object selectById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    @Transactional
    public void updateUser(User user) {
        userMapper.updateById(user);
    }

    @Override
    @Transactional
    public void deleteById(Integer id) {
        userMapper.deleteById(id);
    }


}
